package com.lyz.staticdemo;

/**
 * @ClassName D4_ArrayUtils
 * @Description 工具类
 * @Author 2214361854
 * @Date 2022/2/17 21:31
 * @Version 1.0
 */
public class D4_ArrayUtils {

    private D4_ArrayUtils() {

    }

    /**
     * 数组转字符串输出
     * @param arr
     * @return
     */
    public static String toString(int[] arr) {
        if(arr==null){
            return null;
        }
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            result += (i == arr.length-1?arr[i]:arr[i]+",");
        }
        result += "]";
        return result;
    }
    public static String toString(double[] arr) {
        if(arr==null){
            return null;
        }
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            result += (i == arr.length-1?arr[i]:arr[i]+",");
        }
        result += "]";
        return result;
    }

    /**
     * 去掉最大值与最小值，求平均分
     * @param arr
     * @return
     */
    public static double getAerage(double[] arr){
        if(arr==null){
            return 0;
        }
        double max = arr[0];
        double min = arr[0];
        double sum = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]>max) {
                max = arr[i];
            }
            if (arr[i]<min) {
                min = arr[i];
            }
            sum += arr[i];
        }
        sum = (sum-max-min)/(arr.length - 2);
        return sum;
    }
    public static double getAerage(int[] arr){
        if(arr==null){
            return 0;
        }
        double max = arr[0];
        double min = arr[0];
        double sum = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]>max) {
                max = arr[i];
            }
            if (arr[i]<min) {
                min = arr[i];
            }
            sum += arr[i];
        }
        sum = (sum-max-min)*1.0/(arr.length - 2);
        return sum;
    }

}
